﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.bean.xml.WorkFlow;
using EkStudio.com.bean.xml.WorkFlow.Node;
using System;
using System.Collections.Generic;

namespace EkStudio.com.ui.form
{
    public partial class FlowNextNodeForm : XtraForm
    {

        private WflowModuleItem bean = null;
        private WflowModuleNodeItem CNode = null;
        private WflowModuleNodeLine CLine = null;
        private int LineType = 1;//1：下一节点  2:结果真  3：结果假
        public FlowNextNodeForm(WflowModuleItem it,WflowModuleNodeItem node,int _LineType)
        {
            InitializeComponent();
            bean = it;
            CNode = node;
            LineType = _LineType;
            InitForm();
        }

        private void InitForm()
        {

            #region 初使化下拉框
            ListComboBox.Properties.Items.Clear();
            if (bean != null && bean.FlowNode != null && bean.FlowNode.FlowCommandNode != null && bean.FlowNode.FlowCommandNode.Count > 0)
            {
                foreach(WflowModuleNodeItem node in bean.FlowNode.FlowCommandNode)
                {
                    if (node.NodeType == WflowUtility.NODE_TYPE_START_UP_FLOW) continue;
                    if(node.Key != CNode.Key)
                    {
                        ListComboBox.Properties.Items.Add(node);
                        if(LineType == 1)
                        {
                            if(CNode.NextNode == node.Key)
                            {
                                ListComboBox.SelectedItem = node;
                            }
                        }
                        else if(LineType == 2)
                        {
                            if(CNode.NodeType == WflowUtility.NODE_TYPE_CONDITION
                                &&CNode.NodeFlowCondition!=null&&CNode.NodeFlowCondition.TrueNode==node.Key)
                            {
                                ListComboBox.SelectedItem = node;
                            }
                        }
                        else if(LineType == 3)
                        {
                            if (CNode.NodeType == WflowUtility.NODE_TYPE_CONDITION
                                && CNode.NodeFlowCondition != null && CNode.NodeFlowCondition.FalseNode == node.Key)
                            {
                                ListComboBox.SelectedItem = node;
                            }
                        }
                    }
                }
            }

            #endregion

            #region 初始化方向

            if (CNode.Lines == null) CNode.Lines = new List<WflowModuleNodeLine>();
            if (CNode.Lines.Count > 0)
            {
                WflowModuleNodeLine postion = null;
                for (int i = 0; i < CNode.Lines.Count; i++)
                {
                    WflowModuleNodeLine line = CNode.Lines[i];
                    if(line.Key == Convert.ToString(LineType))
                    {
                        postion = line;
                        break;
                    }
                }
                if(postion != null)
                {
                    CLine = postion;
                    LineCaption.Text = CLine.Caption;
                    //开始
                    if (postion.FromPoint == WflowUtility.POSTION_NODE_TOP)
                    {
                        BeginRadioTop.Checked = true;
                    }else if(postion.FromPoint == WflowUtility.POSTION_NODE_LEFT)
                    {
                        BeginRadioLeft.Checked = true;
                    }else if(postion.FromPoint == WflowUtility.POSTION_NODE_BOTTOM)
                    {
                        BeginRadioButtom.Checked = true;
                    }
                    else
                    {
                        BeginRadioRight.Checked = true;
                    }
                    //贴边
                    if(postion.FromPostion == WflowUtility.POSTION_NODE_LEFT)
                    {
                        BeginToggleSwitch.EditValue = false;
                    }
                    else
                    {
                        BeginToggleSwitch.EditValue = true;
                    }

                    //结束
                    if(postion.ToPoint == WflowUtility.POSTION_NODE_TOP)
                    {
                        EndRadioTop.Checked = true;
                    }
                    else if(postion.ToPoint == WflowUtility.POSTION_NODE_LEFT)
                    {
                        EndRadioLeft.Checked = true;
                    }
                    else if(postion.ToPoint == WflowUtility.POSTION_NODE_BOTTOM)
                    {
                        EndRadioButtom.Checked = true;
                    }
                    else if(postion.ToPoint == WflowUtility.POSTION_NODE_RIGHT)
                    {
                        EndRadioRight.Checked = true;
                    }
                    else
                    {
                        EndRadioRight.Checked = true;
                    }

                    //贴线
                    if (postion.ToPostion == WflowUtility.POSTION_NODE_LEFT)
                    {
                        EndToggleSwitch.EditValue = false;
                    }
                    else
                    {
                        EndToggleSwitch.EditValue = true;
                    }

                }
            }

            #endregion
        }

        private void EnteryButton_Click(object sender, EventArgs e)
        {
            
            if (CNode.Lines == null) CNode.Lines = new List<WflowModuleNodeLine>();
            if (CLine == null)
            {
                CLine = new WflowModuleNodeLine();
                CNode.Lines.Add(CLine);
            }
            WflowModuleNodeItem select = ListComboBox.SelectedItem as WflowModuleNodeItem;
            if (select != null)
            {
                if (LineType == 1)
                {
                    CNode.NextNode = select.Key;
                }else if(LineType == 2)
                {
                    CNode.NodeFlowCondition.TrueNode = select.Key;
                }else if(LineType == 3)
                {
                    CNode.NodeFlowCondition.FalseNode = select.Key;
                }
                CLine.Key = Convert.ToString(LineType);

                //开始位置
                if (BeginRadioTop.Checked)
                {
                    CLine.FromPoint = WflowUtility.POSTION_NODE_TOP;
                }
                else if(BeginRadioLeft.Checked)
                {
                    CLine.FromPoint = WflowUtility.POSTION_NODE_LEFT;
                }
                else if (BeginRadioButtom.Checked)
                {
                    CLine.FromPoint = WflowUtility.POSTION_NODE_BOTTOM;
                }else if (BeginRadioRight.Checked)
                {
                    CLine.FromPoint = WflowUtility.POSTION_NODE_RIGHT;
                }

                if(BeginToggleSwitch.EditValue.Equals(true))
                {
                    CLine.FromPostion = WflowUtility.POSTION_NODE_RIGHT;
                }
                else
                {
                    CLine.FromPostion = WflowUtility.POSTION_NODE_LEFT;
                }

                //结束位置
                if (EndRadioTop.Checked)
                {
                    CLine.ToPoint = WflowUtility.POSTION_NODE_TOP;
                }
                else if (EndRadioLeft.Checked)
                {
                    CLine.ToPoint = WflowUtility.POSTION_NODE_LEFT;
                }
                else if (EndRadioButtom.Checked)
                {
                    CLine.ToPoint = WflowUtility.POSTION_NODE_BOTTOM;
                }
                else if (EndRadioRight.Checked)
                {
                    CLine.ToPoint = WflowUtility.POSTION_NODE_RIGHT;
                }
                //位置
                if (EndToggleSwitch.EditValue.Equals(true))
                {
                    CLine.ToPostion = WflowUtility.POSTION_NODE_RIGHT;
                }
                else
                {
                    CLine.ToPostion = WflowUtility.POSTION_NODE_LEFT;
                }

                CLine.Caption = LineCaption.Text;

            }

        }
    }


    
}